package com.foreknow.elm.controller;

import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartController {
    private CartService service = new CartServiceImpl();
    private Cart cart = new Cart();

    public Object listCart(HttpServletRequest request) throws Exception{
        List<Cart> list;
        String userId = request.getParameter("userId");
        Integer businessId;
        try {
            businessId = Integer.valueOf(request.getParameter("businessId"));
        }catch (Exception e){
            businessId = null;
        }
        list = service.listCart(userId,businessId);
        return list;
    }

    public Object saveCart(HttpServletRequest request) throws Exception{
        int row;
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));

        cart.setFoodId(foodId);
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        row = service.saveCart(cart);
        return row;
    }

    public Object updateCart(HttpServletRequest request) throws Exception{
        int row;
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        cart.setUserId(userId);
        cart.setQuantity(quantity);
        row = service.updateCart(cart);
        return row;
    }

    public Object removeCart(HttpServletRequest request) throws Exception{
        int row;
        Integer foodId;
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        try {
            foodId = Integer.valueOf(request.getParameter("foodId"));
        }catch (Exception e){
            foodId=null;
        }
        cart.setFoodId(foodId);
        row = service.removeCart(cart);
        return row;
    }
}
